import request from '@/config/axios'

export interface LoginReq {
  mobile: string
  password: string
  socialType?: number
  socialCode?: string
  socialState?: string
}

export interface SmsSendReq {
  mobile?: string
  scene: number
}

export interface SmsLoginReq {
  mobile: string
  code: string
  socialType?: number
  socialCode?: string
  socialState?: string
}

export interface LoginResp {
  userId: number
  accessToken: string
  refreshToken: string
  expiresTime: string
  openid?: string
}

const prefix = '/member/auth'

export const login = (data: LoginReq) => {
  return request.post<LoginResp>({ url: `${prefix}/login`, data })
}

export const logout = () => {
  return request.post<boolean>({ url: `${prefix}/logout`, data: {} })
}

export const refreshToken = (refreshTokenStr: string) => {
  return request.post<LoginResp>({ url: `${prefix}/refresh-token?refreshToken=${encodeURIComponent(refreshTokenStr)}`, data: {} })
}

export const sendSmsCode = (data: SmsSendReq) => {
  return request.post<boolean>({ url: `${prefix}/send-sms-code`, data })
}

export const smsLogin = (data: SmsLoginReq) => {
  return request.post<LoginResp>({ url: `${prefix}/sms-login`, data })
}